<?php

/**
 * CrimeWatch - CrimeWatch Controller
 *
 * @author Rogier van der Linde <rogier.vanderlinde@kahosl.be>
 *
 */

class RegisterController extends PlonkController {

	/**
	 * The views allowed for this module
	 * @var array
	 */
	protected $views = array(
		'register', 'success'
	);


	/**
	 * The actions allowed for this module
	 * @var array
	 */
	protected $actions = array(
		'register'
	);


	/**
	 * The form errors
	 * @var array
	 */
	private $formErrors = array();
	
	public function doRegister() {
		// All posted values
		$firstname = trim((string)  PlonkFilter::getPostValue('firstname'));
		$lastname = trim((string)  PlonkFilter::getPostValue('lastname'));
		$username = trim((string)  PlonkFilter::getPostValue('username'));
		$email = trim((string)  PlonkFilter::getPostValue('email'));
		$password = trim((string) PlonkFilter::getPostValue('password'));
		$passwordConfirm = trim((string)  PlonkFilter::getPostValue('passwordConfirm'));
		
		try {
			$this->formErrors = RegisterDB::Register($firstname, $lastname, $username, $email, $password, $passwordConfirm);
		} catch(Exception $e) {
			Plonk::dump($e->getMessage());
			PlonkWebsite::redirect($_SERVER['PHP_SELF'] . '?' . PlonkWebsite::$moduleKey . '=oops');
		}
		// TODO: Send an email for confirmation
	}
	
	public function showRegister() {
		// ---------- Main template stuff ---------- //
		
		$this->setMainTemplate();
		
		// ---------- Page template stuff ---------- //
		$this->pageTpl->assign('formUrl', 'index.php?module=register');
		
		// ---------- Other stuff ---------- //
		if(!$this->formSubmittedCorrectly()) {
			$this->handleErrors();
		} else {
			// TODO: Edit this page
			PlonkWebsite::redirect($_SERVER['PHP_SELF'] . '?' . PlonkWebsite::$moduleKey . '=register' . '&' . PlonkWebsite::$viewKey . '=success');
		}
	}
	
	public function showSuccess() {
		// ---------- Main template stuff ---------- //
		
		$this->setMainTemplate();
		
		// ---------- Page template stuff ---------- //
		
	}
	
	private function formSubmittedCorrectly() {
		if (isset($_POST['register']) && sizeof($this->formErrors) == 0)
			return true;
		else
			return false;
	}
	
	private function handleErrors() {
		if (sizeof($this->formErrors) > 0) {
			$this->pageTpl->assignOption('oErrors');
			$this->pageTpl->setIteration('iErrors');
			foreach ($this->formErrors as $error) {
				$this->pageTpl->assignIteration('error', $error);
				$this->pageTpl->refillIteration();
			}
			$this->pageTpl->parseIteration();
		}
	}
	
	private function setNavigationOptions() {
		if(PlonkSession::exists('id')) { 
			$this->mainTpl->assignOption('oLogout');
		} else { 
			$this->mainTpl->assignOption('oLogin');
			$this->mainTpl->assignOption('oRegister');
		}
	}
	
	private function setMainTemplate() {
		$this->mainTpl->assign('pageTitle', 'Register');
		$this->mainTpl->assign('pageMeta', '<link rel="stylesheet" href="modules/register/css/register.css" />');
		
		$this->setNavigationOptions();
	}

}
// EOF